package com.darksider.darkotlin.ui.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.darksider.darkotlin.R
import com.darksider.darkotlin.bean.Car
import kotlinx.android.synthetic.main.item_name.view.*

/**
 * User: yaodong(1126220529@qq.com)
 * Date: 2017-05-26
 * Time: 14:41
 * Description:
 * Update:
 */
class SimpleRVAdater(var items: List<Car>, var itemClick: (View, Int) -> Unit) : RecyclerView.Adapter<SimpleRVAdater.SimpleViewHolder>() {

    override fun onBindViewHolder(holder: SimpleViewHolder?, position: Int) {
        onBindView(holder!!.itemView, position)
    }

    fun onBindView(itemView: View, position: Int) {
        val name = items[position].CarNo
        itemView.tv_name.text = name
        itemView.setOnClickListener { itemClick(itemView, position) }
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SimpleViewHolder {
        val itemView = LayoutInflater.from(parent!!.context).inflate(R.layout.item_name, parent, false)
        return SimpleViewHolder(itemView)
    }


    fun refreshData(newData: List<Car>){
        items = newData
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = items.size


    class SimpleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}